package com.hncc.dms.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.hncc.bms.domain.BmsBillsRecord;
import com.hncc.bms.domain.BmsInvoiceRecord;
import com.hncc.bms.mapper.BmsBillsRecordMapper;
import com.hncc.bms.mapper.BmsInvoiceRecordMapper;
import com.hncc.sms.domain.SmsSkd;
import com.hncc.sms.mapper.SmsSkdMapper;
import com.ruoyi.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hncc.dms.mapper.DmsRegistrationMapper;
import com.hncc.dms.domain.DmsRegistration;
import com.hncc.dms.service.IDmsRegistrationService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 就诊(门诊)信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-09-04
 */
@Service
@Slf4j
public class DmsRegistrationServiceImpl implements IDmsRegistrationService 
{
    /**
     * 门诊信息
     */
    @Resource
    private DmsRegistrationMapper dmsRegistrationMapper;

    /**
     * 账单流水
     */
    @Resource
    private BmsInvoiceRecordMapper bmsInvoiceRecordMapper;

    /**
     * 发票
     */
    @Resource
    private BmsBillsRecordMapper bmsBillsRecordMapper;

    @Resource
    private SmsSkdMapper smsSkdMapper;

    /**
     * 查询就诊(门诊)信息
     * 
     * @param id 就诊(门诊)信息ID
     * @return 就诊(门诊)信息
     */
    @Override
    public DmsRegistration selectDmsRegistrationById(Long id)
    {
        return dmsRegistrationMapper.selectDmsRegistrationById(id);
    }

    /**
     * 查询就诊(门诊)信息列表
     * 
     * @param dmsRegistration 就诊(门诊)信息
     * @return 就诊(门诊)信息
     */
    @Override
    public List<DmsRegistration> selectDmsRegistrationList(DmsRegistration dmsRegistration)
    {
        return dmsRegistrationMapper.selectDmsRegistrationList(dmsRegistration);
    }

    /**
     * 新增就诊(门诊)信息
     * 
     * @param dmsRegistration 就诊(门诊)信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertDmsRegistration(DmsRegistration dmsRegistration)
    {
        /* 创建时间 */
        dmsRegistration.setCreateTime(DateUtils.getNowDate());
        BmsBillsRecord bmsBillsRecord = new BmsBillsRecord();
        BmsInvoiceRecord bmsInvoiceRecord = new BmsInvoiceRecord();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
        //新增门诊信息
        if (dmsRegistrationMapper.insertDmsRegistration(dmsRegistration)>0){
            bmsBillsRecord.setBillNo(format.format(new Date())+RandomNum());
            bmsBillsRecord.setStatus(1);
            bmsBillsRecord.setInvoiceNum(1);
            bmsBillsRecord.setCreateTime(new Date());
            bmsBillsRecord.setRegistrationId(dmsRegistration.getId());
            //新增流水
            if(bmsBillsRecordMapper.insertBmsBillsRecord(bmsBillsRecord)>0){
                bmsInvoiceRecord.setCreateTime(new Date());
                bmsInvoiceRecord.setInvoiceNo(dmsRegistration.getInvoiceNo());
                bmsInvoiceRecord.setBillId(bmsBillsRecord.getId());
                bmsInvoiceRecord.setAmount(dmsRegistration.getPrice());
                bmsInvoiceRecord.setSettlementCatId(dmsRegistration.getSettlement());
                //新增发票
                bmsInvoiceRecordMapper.insertBmsInvoiceRecord(bmsInvoiceRecord);
                //获取医生排班信息
                SmsSkd smsSkd=smsSkdMapper.selectSmsSkdById(dmsRegistration.getSkdId());
                if (smsSkd != null && smsSkd.getSkLimit()>0) {
                    //修改医生限额
                    smsSkd.setSkLimit(smsSkd.getSkLimit() - 1);
                    smsSkdMapper.updateSmsSkd(smsSkd);
                    return 1;
                }
                return 1;
            }
        }
        return 0;
    }

    /**
     *  四位随机数
     * @return num
     */
    public String RandomNum(){
        String num = "";
        for (int i = 0; i <4 ; i++) {
            num+=(int)(Math.random()*10)+"";
        }
        return num;
    }

    /**
     * 修改就诊(门诊)信息
     * 
     * @param dmsRegistration 就诊(门诊)信息
     * @return 结果
     */
    @Override
    public int updateDmsRegistration(DmsRegistration dmsRegistration)
    {
        return dmsRegistrationMapper.updateDmsRegistration(dmsRegistration);
    }

    /**
     * 批量删除就诊(门诊)信息
     * 
     * @param ids 需要删除的就诊(门诊)信息ID
     * @return 结果
     */
    @Override
    public int deleteDmsRegistrationByIds(Long[] ids)
    {
        return dmsRegistrationMapper.deleteDmsRegistrationByIds(ids);
    }

    /**
     * 删除就诊(门诊)信息信息
     * 
     * @param id 就诊(门诊)信息ID
     * @return 结果
     */
    @Override
    public int deleteDmsRegistrationById(Long id)
    {
        return dmsRegistrationMapper.deleteDmsRegistrationById(id);
    }

    @Override
    public List<DmsRegistration> selectByNameOrMedicalRecordNo(String name, String medicalRecordNo) {
        return dmsRegistrationMapper.selectByNameOrMedicalRecordNo(name, medicalRecordNo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int returnNo(Long id, Long registrationId, Long skdId) {
        //修改挂号表记录状态
        DmsRegistration dmsRegistration=new DmsRegistration();
        dmsRegistration.setId(registrationId);
        dmsRegistration.setStatus(4);
        int result=dmsRegistrationMapper.updateDmsRegistration(dmsRegistration);

        //更新原发票类型
        BmsInvoiceRecord bmsInvoiceRecord=new BmsInvoiceRecord();
        bmsInvoiceRecord.setId(id);
        bmsInvoiceRecord.setType(3);
        int result1=bmsInvoiceRecordMapper.updateBmsInvoiceRecord(bmsInvoiceRecord);

        //插入冲红发票
        BmsInvoiceRecord redInvoiceRecord= bmsInvoiceRecordMapper.selectBmsInvoiceRecordById(id);
        redInvoiceRecord.setId(null);
        redInvoiceRecord.setAmount(redInvoiceRecord.getAmount().negate());
        redInvoiceRecord.setAssociateId(id);
        redInvoiceRecord.setSettlementCatId(null);
        redInvoiceRecord.setItemList(bmsInvoiceRecord.getItemList());
        redInvoiceRecord.setType(2);
        redInvoiceRecord.setCreateTime(DateUtils.getNowDate());
        int result3= bmsInvoiceRecordMapper.insertBmsInvoiceRecord(redInvoiceRecord);

        //对应医生挂号限额+1
        SmsSkd smsSkd=smsSkdMapper.selectSmsSkdById(skdId);
        smsSkd.setSkLimit(smsSkd.getSkLimit()+1);
        int result2=smsSkdMapper.updateSmsSkd(smsSkd);
        if (result>0&&result1>0&&result2>0&&result3>0){
            return 1;
        }
        return 0;
    }
}
